package netty;

import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.concurrent.atomic.AtomicInteger;

@ChannelHandler.Sharable
public class SharableHandler extends ChannelInboundHandlerAdapter {

    private AtomicInteger idx = new AtomicInteger(0);

    SharableHandler() {
        System.out.println("sharable handler construct");
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) {
        System.out.println("当前客户端数为" + idx.incrementAndGet());
        ctx.fireChannelRegistered();
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) {
        System.out.println("当前客户端数为" + idx.decrementAndGet());
        ctx.fireChannelUnregistered();
    }
}
